package edu.pitt.cs3530.scripts;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.Map.Entry;

public class AverageRequests {
	public static void main(String args[]) {
		File directory = new File(args[0]);
		if (directory.isDirectory()) {
			Map<String, List<BufferedReader>> map = new HashMap<String, List<BufferedReader>>();
			for (File file : directory.listFiles()) {
				StringTokenizer tok = new StringTokenizer(file.getName(), "-");
				if (tok.countTokens() == 3) {
					StringBuffer sb = new StringBuffer();
					tok.nextToken();
					String token = tok.nextToken();
					sb.append(token);
					if (isNumeric(token)) {
						if (!map.containsKey(sb.toString())) {
							map.put(sb.toString(),
									new ArrayList<BufferedReader>());
						}
						try {
							map.get(sb.toString()).add(
									new BufferedReader(new FileReader(file)));
						} catch (FileNotFoundException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					} else {
						while (tok.hasMoreTokens()) {
							token = tok.nextToken();
							sb.append("-").append(token);
							if (isNumeric(token)) {
								if (!map.containsKey(sb.toString())) {
									map.put(sb.toString(),
											new ArrayList<BufferedReader>());
								}
								try {
									map.get(sb.toString()).add(
											new BufferedReader(new FileReader(
													file)));
								} catch (FileNotFoundException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
							}
						}
					}
				}
			}
			for (Entry<String, List<BufferedReader>> entry : map.entrySet()) {
				try {
					BufferedWriter writer = new BufferedWriter(new FileWriter(
							new File(directory, entry.getKey())));
					boolean hasMore = true;
					int seconds = 0;
					while (hasMore) {
						hasMore = false;
						double totalPower = 0.0;
						int count = 0;
						for (BufferedReader reader : entry.getValue()) {
							if (reader.ready()) {
								hasMore = true;
								String line = reader.readLine();
								if ((line != null)
										&& (line.trim().length() != 0)) {
									StringTokenizer tok = new StringTokenizer(
											line, ":");
									if (tok.nextToken().equals("Time")) {
										continue;
									}
									double watts = Double.parseDouble(tok
											.nextToken());
									System.out.println(watts);
									totalPower += watts;
									count++;
								}
							}
						}
						seconds += 5;
						StringBuilder sb = new StringBuilder();
						sb.append(seconds);
						sb.append(',');
						sb.append((totalPower / (double) count));
						sb.append("\r\n");
						writer.write(sb.toString());
					}
					writer.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	public static boolean isNumeric(String string) {
		boolean retval = true;
		if ((string == null) || (string.length() == 0)) {
			retval = false;
		}
		if (retval) {
			for (int i = 0; i < string.length(); i++) {
				if (!Character.isDigit(string.charAt(i))) {
					retval = false;
					break;
				}
			}
		}
		return retval;
	}
}
